//
//  CircleButton.m
//  WOFishingBoat
//
//  Created by 郑逸凯 on 2019/1/4.
//  Copyright © 2019年 郑逸凯. All rights reserved.
//

#import "CircleButton.h"

@interface CircleButton ()
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UIImageView *backImageView;
@property (nonatomic,strong)UIImageView *centerImageView;
@property (nonatomic,strong)UIControl *topControl;
@end

@implementation CircleButton

- (instancetype)initWithImageName:(NSString *)imageName title:(NSString *)title {
    if (self = [super init]) {
        [self addSubview:self.backImageView];
        [self addSubview:self.centerImageView];
        [self addSubview:self.titleLabel];
        [self addSubview:self.topControl];
        self.centerImageView.image = [UIImage imageNamed:imageName];
        self.titleLabel.text = title;
    }
    return self;
}

- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents {
    [self.topControl addTarget:target action:action forControlEvents:controlEvents];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    self.backImageView.frame = CGRectMake(0, 0, width, width);
    self.centerImageView.frame = CGRectMake(width / 3, width / 3, width / 3, width / 3);
    self.titleLabel.frame = CGRectMake(0, height - 20, width, 20);
    self.topControl.frame = self.bounds;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.textColor = RGB16(0x999999);
        label.font = [UIFont systemFontOfSize:12];
        label.textAlignment = NSTextAlignmentCenter;
        _titleLabel = label;
    }
    return _titleLabel;
}

- (UIImageView *)centerImageView {
    if (!_centerImageView) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.userInteractionEnabled = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        _centerImageView = imageView;
    }
    return _centerImageView;
}

- (UIImageView *)backImageView {
    if (!_backImageView) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mon_small_circle"]];
        imageView.userInteractionEnabled = YES;
        _backImageView = imageView;
    }
    return _backImageView;
}

- (UIControl *)topControl {
    if (!_topControl) {
        _topControl = [[UIControl alloc] init];
    }
    return _topControl;
}


@end
